package com.cq.service.impl;

import com.alibaba.excel.EasyExcel;
import com.cq.excelModel.ActivityPlatformInfluencertypeGroupExcel;
import com.cq.mapper.ActivityPlatformInfluencertypeGroupMapper;
import com.cq.pojo.ActivityPlatformInfluencertypeGroup;
import com.cq.pojo.Alog;
import com.cq.query.PageResultQuery;
import com.cq.result.PageResult;
import com.cq.service.ActivityPlatformInfluencertypeGroupService;
import com.cq.utils.ExcelUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class ActivityPlatformInfluencertypeGroupServiceImpl implements ActivityPlatformInfluencertypeGroupService {

    @Autowired
    private ActivityPlatformInfluencertypeGroupMapper activityPlatformInfluencertypeGroupMapper;

    @Override
    public PageResult<ActivityPlatformInfluencertypeGroup> findActivityPlatformInfluencertypeGroup(PageResultQuery query) {
        PageHelper.startPage(query.getPageNum(), query.getPageSize());

        List<ActivityPlatformInfluencertypeGroup> list = activityPlatformInfluencertypeGroupMapper.selectActivityPlatformInfluencertypeGroup(query);

        PageInfo<ActivityPlatformInfluencertypeGroup> pageInfo = new PageInfo<>(list);

        return new PageResult<>(pageInfo.getPageNum(), pageInfo.getPageSize(), pageInfo.getTotal(), list);

    }

    @Override
    public void activityPlatformInfluencertypeGroupDownload(HttpServletResponse response, PageResultQuery query) throws IOException {
        PageResult<ActivityPlatformInfluencertypeGroup> result = this.findActivityPlatformInfluencertypeGroup(query);
        List<ActivityPlatformInfluencertypeGroup> list = result.getList();

        String fileName = System.currentTimeMillis() + "";

        List<ActivityPlatformInfluencertypeGroupExcel> collect = list.stream()
                .map(a -> {
                    ActivityPlatformInfluencertypeGroupExcel excel = new ActivityPlatformInfluencertypeGroupExcel();
                    BeanUtils.copyProperties(a, excel);
                    Alog alog = a.getAlog();
                    BeanUtils.copyProperties(alog, excel);
                    return excel;
                }).collect(Collectors.toList());

        EasyExcel.write(response.getOutputStream(), ActivityPlatformInfluencertypeGroupExcel.class)
                .registerWriteHandler(ExcelUtil.getHorizontalCellStyleStrategy(response, fileName))
                .sheet("分活动分媒介分达人类型")
                .doWrite(collect);
    }
}
